<?php
namespace app\api\controller;
use think\Controller;
use think\Db;

class Details extends Controller{
  //处理跨域请求
    public function __construct(){
        header('Access-Control-Allow-Origin:*');
        header('Access-Control-Allow-Methods:GET, POST, OPTIONS');
    } 
    //查询单条房源详情
    public function selhouse(){
        if(!empty($_POST['houseid'])){
            $houseid = $_POST['houseid'];
            $res = Db::name('houseinfo')
                ->where('id',$houseid)
                ->select();
            return json($res);
        }
    }
    //查询看过这个房源的人还看过什么
    public function aginhouse() {
        $res = Db::name('houseinfo')
        ->limit(4)
        ->select();
        return json($res);
    }
    //查询单条新闻详情
    public function selnews(){
        if(!empty($_POST['newsid'])){
            $newsid = $_POST['newsid'];
            $res = Db::name("journalisminfo")
            ->where("id",$newsid)
            ->select();
            return json($res);
        }
    }
    //查询热门新闻详情
    public function selhotnews(){
            $res = Db::name("journalisminfo")
            ->limit(3)
            ->select();
            return json($res);
    }
    //查询热门写字楼
    public function hothouse(){
        $res = Db::name("houseinfo")
        ->where('house_type',1)
        ->limit(4)
        ->select();
         return json($res);
    }
    //头部查询
    public function selhousetype() {
        $type=$_POST["housetype"];
        $res = Db::name('housetypeinfo')
         ->field('type_content')
         ->where('type_content','like','%'.$type.'%')
         ->select();
        return json($res);
    }
    //添加预约
    public function addyuyue(){
        $houseid = $_POST['houseid'];
        $userid = $_POST['userid'];
        $result = Db::name('reserveinfo')
        ->where('house_id = '.$houseid.' and user_id = '.$userid)
        ->find();
        if(empty($result)){
            $time = date("Y-m-d  h:i:s");
            $state = 0;
            $data=[
                'house_id'=>$houseid,
                'user_id' =>$userid,
                'reserve_time'=>$time,
                'reserve_sta'=>$state
            ];
            $res = Db::name('reserveinfo')->insert($data);
            if($res>0){
                return "1";
            }
        }

    }
}

?>